using System.Collections.Generic;
using Gameplay.PVE.Data;
using UnityEngine;

namespace Gameplay.PVE
{
    public class RpgRegionAttribute : MonoBehaviour
    {
        public int regionId;
        public int pveRegionId;
        public List<Transform> pathList = new List<Transform>();
        [HideInInspector] public RpgBattleRegion region;

        public void Init(int id, RpgBattleRegion region)
        {
            regionId = id;
            this.region = region;
            for (int i = 0; i < region.pathList.Count; i++)
            {
                AddPoint(region.pathList[i],i);
            }
        }
        
        public void Init(int id)
        {
            regionId = id;
            region = new RpgBattleRegion();
            region.id = id;
            region.pathList = new List<Vector3>();
        }
        
        public void AddPoint()
        {
            var position = transform.position;
            int index = -1;
            if (pathList.Count > 0)
            {
                index = pathList.Count - 1;
                position = pathList[index].position;
                AddPoint(position, index + 1);
            }
            else
            {
                AddPoint(position, 0);
            }
        }

        private void AddPoint(Vector3 position,int index)
        {
            var point = GameObject.CreatePrimitive(PrimitiveType.Cube);
            point.name = "GoToRpgPathPoint" + index;
            point.transform.SetParent(transform);
            point.transform.position = position;
            pathList.Add(point.transform);
        }
        
        public void SaveRegion()
        {
            region.pathList.Clear();
            for (int i = 0; i < pathList.Count; i++)
            {
                region.pathList.Add(pathList[i].transform.position);
            }
            if (pathList.Count > 0)
            {
                region.checkPosition = pathList[0].transform.position;
                region.battlePosition = pathList[pathList.Count - 1].transform.position;
            }
            region.pveRegionId = pveRegionId;
        }
    }
}